package com.jddemo.utils;

import com.jddemo.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class HtmlParseUtil {


    public static void main(String[] args) throws IOException {
        // 不支持中文
        List<Content> contents = new HtmlParseUtil().parseJD("逻辑思维");
        for (Content content:contents) {
            System.out.println(content.toString());
        }

    }

    public List<Content> parseJD(String keywords) throws IOException {
        // 获得请求 https://search.jd.com/Search?keyword=java
        String url ="https://search.jd.com/Search?keyword="+keywords+"&enc=utf-8";
        // 解析网页 返回html页面
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");

        List<Content> goodsList = new ArrayList<Content>();

        for(Element el: elements){
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();

           Content content = new Content();
           content.setImg(img);
           content.setPrice(price);
           content.setTitle(title);
            goodsList.add(content);
        }
        return goodsList;
    }


}
